package com.hy.util;

/**
 * @作用 生成算式
 * @作者 坏银
 * @时间 2021/10/28 8:43
 */

import cn.hutool.core.util.RandomUtil;
import com.hy.vo.Formula;

import java.util.ArrayList;

public class GetFormula {

    /**
     * @作用 随机生成算式，返回数据库实体类
     * @return Formula
     */
    public static Formula toGetFormula() {
        int num = RandomUtil.randomInt(0,3);
        if(num==0){
            return toGetAdditionFormula();
        }else if (num==1){
            return toGetSubtractionFormula();
        }else if(num == 2){
            return toGetMultiplicationFormula();
        }else {
            return toGetDivisionFormula();
        }
    }

    /**
     * @作用 随机生成加法算式，返回数据库实体类
     * @return Formula
     */
    public static Formula toGetAdditionFormula() {
        Formula formula = new Formula();
        int forward = RandomUtil.randomInt(0,100);
        int behind = RandomUtil.randomInt(0,100);
        int ans = forward + behind;
        while(ans > 100){
            forward = RandomUtil.randomInt(0,100);
            behind = RandomUtil.randomInt(0,100);
            ans = forward + behind;
        }
        formula.setForward(forward);
        formula.setSymbol('+');
        formula.setBehind(behind);
        formula.setAnswer(ans);
        return formula;
    }

    /**
     * @作用 随机生成减法算式，返回数据库实体类
     * @return Formula
     */
    public static Formula toGetSubtractionFormula() {
        Formula formula = new Formula();
        int forward = RandomUtil.randomInt(0,100);
        int behind = RandomUtil.randomInt(0,100);
        if (forward < behind) {
            forward = forward + behind;
            behind = forward - behind;
            forward = forward - behind;
        }
        int ans = forward - behind;
        formula.setForward(forward);
        formula.setBehind(behind);
        formula.setAnswer(ans);
        formula.setSymbol('-');
        return formula;
    }

    /**
     * @作用 随机生成乘法算式，返回数据库实体类
     * @return Formula
     */
    public static Formula toGetMultiplicationFormula() {
        Formula formula = new Formula();
        int forward = RandomUtil.randomInt(0,100);
        int behind = RandomUtil.randomInt(0,100);
        int ans = forward * behind;
        while(ans > 100){
            forward = RandomUtil.randomInt(0,100);
            behind = RandomUtil.randomInt(0,100);
            ans = forward * behind;
        }
        formula.setForward(forward);
        formula.setSymbol('*');
        formula.setBehind(behind);
        formula.setAnswer(ans);
        return formula;
    }

    /**
     * @作用 随机生成除法算式，返回数据库实体类
     * @return Formula
     */
    public static Formula toGetDivisionFormula() {
        Formula formula = new Formula();
        int forward =RandomUtil.randomInt(0,100);
        int behind = RandomUtil.randomInt(0,100);
        int ans;
        //log.info("forward:"+forward+"  behind:"+behind);
        //如果尾数等于0，那么前者给到0，后者给到1-100的一个随机数
        if (behind == 0) {
            behind = RandomUtil.randomInt(1,100);
            forward = 0;
        }
        if (forward % behind == 0) {
            ans = forward / behind;
        } else {
            /**
             * 如果除不下或者behind等于0
             * 思路：使用第一个数，遍历出所有的因子，包括1和他本身，再随机选一个
             * v2：因为素数过多，我们尽量保证有因子的，直接排除1和他本身输出
             */
            if (forward == 1) {
                behind = 1;
                ans = 1;
            } else {
                ArrayList<Integer> list = new ArrayList<>();
                int factor = 1;
                //标识符，用来判断是否是有因子
                boolean flag = false;
                for (int i = 2; i < forward; i++) {
                    //如果是因子就丢入因子数组中
                    if (forward % i == 0) {
                        flag = true;
                        list.add(i);
                        factor++;
                    }
                }
                //如果没有因子
                if (!flag) {
                    //添加他本身和1
                    list.add(1);
                    list.add(forward);
                    factor = 2;
                }
                //随机生成一个数

                behind = list.get(RandomUtil.randomInt(0,factor - 1));
                ans = forward / behind;
            }
        }
        //丢入对象
        formula.setForward(forward);
        formula.setBehind(behind);
        formula.setAnswer(ans);
        formula.setSymbol('/');
        return formula;
    }
}
